package org.smile.homework02.hyl;

import lombok.Data;

import java.util.ArrayList;

@Data
public class ParkingLot {

    //1.0生成停车场
    public ParkingLot(Integer parkingSpaceNumber) {
        this.parkingSpaces = new ArrayList<>(); //停车位
        for (int i = 0; i < parkingSpaceNumber; i++) {
            ParkingSpace parkingSpace = new ParkingSpace();
            parkingSpace.describe();
            this.parkingSpaces.add(parkingSpace);
        }
    }

    //1.1生成停车位（若干 -> 数组）
    private ArrayList<ParkingSpace> parkingSpaces; //外部传值，如给个5，就是5个停车位

    private Integer total = 0;

    //记录进入停车场的车辆
    //记录时间
//    private ArrayList<VehicleMark> vehicleMarks = new ArrayList<>();  //null，获得车，和时间，

    //1.2收费标准(方法)
    public Integer countingPrice(Vehicle vehicle) {
        String type = vehicle.getType();
        switch (type) {
            case "大":
                return 15;
            case "中":
                return 10;
            case "小":
            default:
                return 5;
        }
    }//按照车的类型收费，需要一个车的传参，获取车的类型给string type， 返回钱的值， vehicle 没有在parkinglot中
    //定义，是不是用的是space。

    //===================
    //***形成交互
    //*承载交互的变量
    //*维护变量更新的方法

    //记录进入停车场的车辆
    //记录时间
    public void handleEnterParkingLot(Vehicle vehicle, Integer time) {//外界传给的车
//        VehicleMark vehicleMark = new VehicleMark();
//        vehicleMark.setVehicle(vehicle);//车
//
//        vehicleMark.setTime(time); //车的时间
//        this.vehicleMarks.add(vehicleMark);//添加到mark中，mark中有某台车的时间了
    }

//    public void handleLeaveParkingLot(Vehicle vehicle) {
//        ArrayList<Integer> remarks = new ArrayList<>();
//        for (int i = 0; i < this.vehicleMarks.size(); i++) {
//            VehicleMark vehicleMark = this.vehicleMarks.get(i);
//            if (vehicleMark.getVehicle().getNumber().equals(vehicle.getNumber())) {
//                remarks.add(i);
//            }
//        }
//
//        for (int i = 0; i < remarks.size(); i++) {
//            this.vehicleMarks.remove(remarks.get(i));
//        }
//    }

    public void handleLeaveParkingLot(Vehicle vehicle, Integer time) {
        total += countAmount(vehicle, time);//算钱
//       // 车离开删除车的记录
//        ArrayList<Integer> remarks = new ArrayList<>();
//        for (int i = 0; i < this.vehicleMarks.size(); i++) {
//            VehicleMark vehicleMark = this.vehicleMarks.get(i);
//            if (vehicleMark.getVehicle().getNumber().equals(vehicle.getNumber())) {
//                remarks.add(i);
//            }
//        }
//
//        for (int i = 0; i < remarks.size(); i++) {
//            this.vehicleMarks.remove(remarks.get(i));
//        }
    }

    //车辆进入停车位的时候，车与停车位交互，那辆车与停在哪个位置，parkingspacenumber是车位。
    public void handleEnterParkingSpace(Vehicle vehicle, Integer parkingSpaceNumber) {
        ParkingSpace parkingSpace = this.parkingSpaces.get(parkingSpaceNumber - 1);
        parkingSpace.handleInParkingSpace(vehicle);
    }
    //离开停车位，
    public void handleLeaveParkingSpace(Integer parkingSpaceNumber) {
        ParkingSpace parkingSpace = this.parkingSpaces.get(parkingSpaceNumber - 1);
        parkingSpace.handleOutParkingSpace();
    }


    //1.3计算停车费用（方法）
    public Integer countAmount(Vehicle vehicle, Integer time) {
//        VehicleMark vehicleMark = new VehicleMark();
//        vehicleMark.setVehicle(vehicle);
//        int i = this.vehicleMarks.indexOf(vehicleMark);

//        VehicleMark vehicleMark = new VehicleMark();
//
//        for (VehicleMark vehicleMark1 : this.vehicleMarks) {
//            if (vehicleMark1.getVehicle().getNumber().equals(vehicle.getNumber())) {
//                vehicleMark = vehicleMark1;//车牌号码相同
//            }
//        }
        //进入停车场time
        Integer recordTime = vehicle.getEnterTime();
        //停留时间
        int costTime = time - recordTime;
        return countingPrice(vehicle) * costTime;//算费用
    }

    public void runBusiness(Integer time) {
        for (ParkingSpace parkingSpace : this.getParkingSpaces()) {
            if (parkingSpace.isOccupied()) {
                if (parkingSpace.getVehicle().decideLeave()) {
                    Vehicle vehicle = parkingSpace.getVehicle();
                    vehicle.leaveParkingSpace(this, parkingSpace.getNumber());
                    vehicle.leaveParkingLot(this, time);
                }
            }
        }
    }

    //显示停车场状态（停车位、车辆）
    public void describe() {
        System.out.println("====================停车场状态========================");
        for (ParkingSpace parkingSpace : this.getParkingSpaces()) {
            parkingSpace.show();
        }
        System.out.println("当前总收入： " + this.getTotal());
        System.out.println("====================*********========================");
    }
}
